package wendyeq.iweb.blog.web.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import wendyeq.iweb.blog.pojo.Visitor;
import wendyeq.iweb.blog.service.IVisitorService;

@Controller
@RequestMapping("/admin/visitor")
public class VisitorController {
	private static final Logger logger = Logger.getLogger(VisitorController.class);
	
	@Resource(name="visitorServiceImpl")
	private IVisitorService visitorService;
	
	@RequestMapping("")
	public String visitor(HttpServletRequest request, ModelMap model) {
		return "admin/visitor";
	}
	
	@RequestMapping("del/{id}")
	public String delVisitor(@PathVariable String id, HttpServletRequest request, ModelMap model) {
		logger.warn("id="+id);
		this.visitorService.delete(id);
		return "admin/visitor";
	}
	
	@RequestMapping("list/{page}")
	public String listVisitor(@PathVariable int page, HttpServletRequest request, ModelMap model) {
		logger.warn("-"+page+"-");
		List<Visitor> vs = this.visitorService.findAll();
		int visSize = vs.size();
		if(visSize<15){
			model.addAttribute("visitors",vs );
		}else if(vs.size()-page*15>=0){
			model.addAttribute("visitors",vs.subList((page-1)*15,page*15));
		}else {
			model.addAttribute("visitors",vs.subList((page-1)*15,visSize));
		}
		model.addAttribute("visSize", visSize);
		model.addAttribute("page",page);
		return "admin/visitorList";
	}
	
}
